<template>
  <div>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
  </van-list>
  </div>
</template>

<script>
import { getSearchResults } from '@/api/search'
export default {
  name: 'SearchResults',
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      page: 1,
      list: [],
      loading: false,
      finished: false
    }
  },

  created () {

  },

  methods: {

    async  onLoad () {
      try {
        const res = await getSearchResults({
          page: this.page,
          per_page: 12,
          q: this.searchText
        })
        if (Math.random() > 0.5) {
          console.lg('错误测试')
        }
        console.log(res)
        // 2  保存数据
        this.list.push(...res.results)
        // 3  加载状态结束
        this.loading = false
        // 4  数据是否全部加载完成
        if (!res.results.length) {
          this.finished = true
        } else {
          this.page++
        }
      } catch (err) {
        this.error = true
        this.loading = false
      }
      // 1  发送请求
      // 2  保存数据
      // this.list.push(this.list.length + 1)
      // 3  加载状态结束
      // this.loading = false
      // 4  数据是否全部加载完成
      //   this.finished = true
    }
  }
}
</script>

<style scoped lang='less'>

</style>
